<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  <meta name="generator" content="Hugo 0.98.0" />

  
  <meta name="description" content="走在通往幸福的路上">
  

  
  <link rel="apple-touch-icon" sizes="180x180" href="https://blog.v5u.win/apple-touch-icon.png">

  
  <link rel="icon" type="image/png" sizes="32x32" href="https://blog.v5u.win/favicon-32x32.png">

  
  <link rel="icon" type="image/png" sizes="16x16" href="https://blog.v5u.win/favicon-16x16.png">

  
  <link rel="manifest" href="https://blog.v5u.win/site.webmanifest">

  
  <link rel="mask-icon" href="https://blog.v5u.win/safari-pinned-tab.svg" color="">

  <meta name="msapplication-TileColor" content="">

  <meta name="theme-color" content="">

  
  <link rel="stylesheet" href="https://blog.v5u.win/css/bootstrap.min.css" />

  
  <title>Go JSON | 为吾优</title>
  

  <style>
body {
  min-width: 300px;
}

.custom-navbar {
  margin-bottom: 1em;
  height: 60px;
}

.custom-navbar a {
  display: inline-block; 
  padding: 18px 0;
  margin-right: 1em; 
  font-weight: bold; 
}

.custom-navbar a:hover,
.custom-navbar a:focus {
  text-decoration: none; 
}

@media print {
  .custom-navbar {
    display: none;
  }
}

article {
  padding-bottom: 1em;
}

img {
  max-width: 100%;
}


body {
  background-color: #fff;
}



body {
  color: #212529;
}



a {
  color: #007bff;
}



a:hover,
a:focus {
  color: #0056b3;
}



.custom-navbar {
  background-color: #212529;
}



.custom-navbar a {
  color: rgba(255,255,255,.75);
}



.custom-navbar a:hover,
.custom-navbar a:focus {
  color: rgba(255,255,255,1);
}



.container {
  max-width: 800px;
}





</style>
</head>

<body>
  <nav class="custom-navbar">
  <div class="container">
    
    <a href="/">文章</a>
    
    <a href="/tags/">标签</a>
    
    <a href="/about/">关于</a>
    
    <a href="/index.xml">RSS</a>
    
  </div>
</nav>
  
  <div class="container">
    <article>
      <h1>Go JSON</h1>
<p>标准库</p>
<pre tabindex="0"><code>encoding/json、encoding/xml、encoding/asn1
</code></pre><p>Model</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Movie</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Title</span>  <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Year</span>   <span style="color:#66d9ef">int</span>  <span style="color:#e6db74">`json:&#34;released&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Color</span>  <span style="color:#66d9ef">bool</span> <span style="color:#e6db74">`json:&#34;color,omitempty&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Actors</span> []<span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 赋值
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">movies</span> = []<span style="color:#a6e22e">Movie</span>{
</span></span><span style="display:flex;"><span>    {<span style="color:#a6e22e">Title</span>: <span style="color:#e6db74">&#34;Casablanca&#34;</span>, <span style="color:#a6e22e">Year</span>: <span style="color:#ae81ff">1942</span>, <span style="color:#a6e22e">Color</span>: <span style="color:#66d9ef">false</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">Actors</span>: []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;Humphrey Bogart&#34;</span>, <span style="color:#e6db74">&#34;Ingrid Bergman&#34;</span>}},
</span></span><span style="display:flex;"><span>    {<span style="color:#a6e22e">Title</span>: <span style="color:#e6db74">&#34;Cool Hand Luke&#34;</span>, <span style="color:#a6e22e">Year</span>: <span style="color:#ae81ff">1967</span>, <span style="color:#a6e22e">Color</span>: <span style="color:#66d9ef">true</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">Actors</span>: []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;Paul Newman&#34;</span>}},
</span></span><span style="display:flex;"><span>    {<span style="color:#a6e22e">Title</span>: <span style="color:#e6db74">&#34;Bullitt&#34;</span>, <span style="color:#a6e22e">Year</span>: <span style="color:#ae81ff">1968</span>, <span style="color:#a6e22e">Color</span>: <span style="color:#66d9ef">true</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">Actors</span>: []<span style="color:#66d9ef">string</span>{<span style="color:#e6db74">&#34;Steve McQueen&#34;</span>, <span style="color:#e6db74">&#34;Jacqueline Bisset&#34;</span>}},
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在编码时，默认使用Go语言结构体的成员名字作为JSON的对象（通过reflect反射技术，我们将在12.6节讨论）。只有导出的结构体成员才会被编码，这也就是我们为什么选择用大写字母开头的成员名称。</p>
<p>即使对应的JSON对象名是小写字母，每个结构体的成员名也是<strong>声明为大写字母开头的</strong>。因为有些JSON成员名字和Go结构体成员名字并不相同，因此需要Go语言结构体成员<strong>Tag来指定对应的JSON名字</strong>。同样，在解码的时候也需要做同样的处理，</p>

    </article>
  </div>

  
  
  

  
</body>

</html>